﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSQuaTrinhGianDoanBaoHiem : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string ThoiGian { get; set; }
        public void EditQTGD(int hdId, DateTime ngayBatDau, DateTime ngayKetThuc, string ghiChu, string lyDo)
        {
            List<NSQuaTrinhGianDoanBaoHiem> lst = (List<NSQuaTrinhGianDoanBaoHiem>)HttpContext.Current.Session["QTGD"];
            NSQuaTrinhGianDoanBaoHiem pc = lst.Where(a => a.QuaTrinhGianDoanBaoHiemID.Equals(hdId)).SingleOrDefault();
            pc.ThoiGianBatDau = ngayBatDau;
            pc.ThoiGianKetThuc = ngayKetThuc;
            pc.GhiChu = ghiChu;
            pc.LyDoGianDoan = lyDo;

        }
        public void DeleteQTGD(int hdId)
        {
            List<NSQuaTrinhGianDoanBaoHiem> lst = (List<NSQuaTrinhGianDoanBaoHiem>)HttpContext.Current.Session["QTGD"];
            NSQuaTrinhGianDoanBaoHiem pc = lst.Where(a => a.QuaTrinhGianDoanBaoHiemID.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllQTGDByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSQuaTrinhGianDoanBaoHiems
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSQuaTrinhGianDoanBaoHiems.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateQTGD_NhanVien(int nhanVienId, List<NSQuaTrinhGianDoanBaoHiem> nblObject)
        {

            var queryNBLByNhanVienId = (from a in db.NSQuaTrinhGianDoanBaoHiems
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSQuaTrinhGianDoanBaoHiems.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSQuaTrinhGianDoanBaoHiem pc in nblObject)
            {
                NSQuaTrinhGianDoanBaoHiem item = new NSQuaTrinhGianDoanBaoHiem();
                CopyNSQuaTrinhGianDoanBaoHiem(pc, item);
                db.NSQuaTrinhGianDoanBaoHiems.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSQuaTrinhGianDoanBaoHiem(NSQuaTrinhGianDoanBaoHiem src, NSQuaTrinhGianDoanBaoHiem des)
        {
            des.LyDoGianDoan = src.LyDoGianDoan;
            des.ThoiGianBatDau = src.ThoiGianBatDau;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.ThoiGianKetThuc = src.ThoiGianKetThuc;

        }
        public List<NSQuaTrinhGianDoanBaoHiem> LoadQTGDByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSQuaTrinhGianDoanBaoHiems
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.ThoiGianBatDau descending
                         select a).ToList();
            foreach (NSQuaTrinhGianDoanBaoHiem item in query)
            {

                item.ThoiGian = string.Format("{0}-{1}", item.ThoiGianBatDau.ToString("dd/MM/yyyy"), item.ThoiGianKetThuc.ToString("dd/MM/yyyy"));
            }
            return query;

        }
        public NSQuaTrinhGianDoanBaoHiem GetQTGD(int nhanVienId, DateTime ngayBatDau, DateTime ngayKetThuc, string ghiChu, string lyDo)
        {

            var db = new DataClassesDataContext();
            NSQuaTrinhGianDoanBaoHiem hd = new NSQuaTrinhGianDoanBaoHiem();
            hd.QuaTrinhGianDoanBaoHiemID = 0 - DateTime.Now.Second;
            hd.ThoiGianBatDau = ngayBatDau;
            hd.ThoiGianKetThuc = ngayKetThuc;
            hd.GhiChu = ghiChu;
            hd.NhanVienID = nhanVienId;
            hd.LyDoGianDoan = lyDo;
            hd.ThoiGian = string.Format("{0}-{1}", ngayBatDau.ToString("dd/MM/yyyy"), ngayKetThuc.ToString("dd/MM/yyyy"));
            return hd;
        }
    }
}